/*
 * bsp.c
 *
 *  Created on: Oct 13, 2011
 *      Author: Yifei
 */

#include "stdio.h"
#include "Point/point.h"

#define DIVISON 20
float splinex[DIVISON-1];
float spliney[DIVISON-1];

point *p1, *p2, *p3, *p4;
void bsp(point p1, point p2, point p3, point p4, int divison);

/*
int main() {

	printf("HelloWorld");

	point p[5];
	p1 = pointAlloc(1.0, 1.0);
	p2 = pointAlloc(15.0, 20.0);
	p3 = pointAlloc(3.0, 3.0);
	p4 = pointAlloc(5.8, 5.0);

	p[1] = *p1;
	p[2] = *p2;
	p[3] = *p3;
	p[4] = *p4;

	int var;
	for (var = 0; var < 5; ++var) {
		printf("The point is %d: (%f,%f) \n", var, p[var].x, p[var].y);
	}
	int n = DIVISON;
	bsp(*p1,*p2,*p3,*p4,n);

	for (var = 0; var < DIVISON; ++var) {
		printf("Spline: (%f,%f)\n",splinex[var],spliney[var]);
	}

return 0;
}
*/
//For 4 Controller
void bsp(point p1, point p2, point p3, point p4, int divison) {
	double a[5];
	double b[5];
	a[0] = (-p1.x + 3 * p2.x - 3 * p3.x + p4.x) / 6.0;
	a[1] = (3 * p1.x - 6 * p2.x + 3 * p3.x) / 6.0;
	a[2] = (-3 * p1.x + 3 * p3.x) / 6.0;
	a[3] = (p1.x + 4 * p2.x + p3.x) / 6.0;
	b[0] = (-p1.y + 3 * p2.y - 3 * p3.y + p4.y) / 6.0;
	b[1] = (3 * p1.y - 6 * p2.y + 3 * p3.y) / 6.0;
	b[2] = (-3 * p1.y + 3 * p3.y) / 6.0;
	b[3] = (p1.y + 4 * p2.y + p3.y) / 6.0;
	splinex[0] = a[3];
	spliney[0] = b[3];
	int i ;
	//Choose 4 controller for the calculation
	for (i = 1;  i <= divison -1 ; i++) {
		float t = i / divison;
		splinex[i] = (a[2] + t * (a[1] + t * a[0]))*t+a[3] ;
		spliney[i] = (b[2] + t * (b[1] + t * b[0]))*t+b[3] ;
	}

}



